package com.bbzn.device.client.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.config.EmqCode;
import com.bbzn.device.client.dataobject.Device;
import com.bbzn.device.client.dto.ct.data.GeneralNbPayload;
import com.bbzn.device.client.dto.ct.data.YgPayload;
import com.bbzn.device.client.dto.ct.data.YgServiceData;
import com.bbzn.device.client.dto.ct.response.TupDataReport;
import com.bbzn.device.client.service.CwtingMessageStrategy;
import com.bbzn.device.client.service.DeviceService;
import com.bbzn.device.client.service.PublishService;
import com.bbzn.device.client.utils.Base64Util;
import com.bbzn.device.client.utils.HexUtil;
import com.bbzn.device.client.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * @Author wulongbo
 * @Date 2021/9/22 10:27
 * @Version 1.0
 */

@Component
@Slf4j
public class DataReportStrategy implements CwtingMessageStrategy {

    @Autowired
    private PublishService publishService;

    @Autowired
    private DeviceService deviceService;

    @Override
    public void pushMessage(JSONObject jsonObject) throws Exception {
        TupDataReport tupDataReport = JSONObject.toJavaObject(jsonObject, TupDataReport.class);
        Object payload = tupDataReport.getPayload();

        JSONObject json = JSONObject.parseObject(payload.toString());
        String APPdata = json.getString("APPdata");
        if (StringUtils.isNotEmpty(APPdata)) {
            String data = Base64Util.encode(APPdata);
            boolean result = JsonUtils.isJSON(data);
            if (result) {
                //NB设备
                publishService.sendToMqtt(data, EmqCode.sendBridgeTopic + tupDataReport.getIMEI() + EmqCode.endNbTopic, 2);
                return;
            }

            data = Base64Util.toHex(APPdata);
            log.info("data:" + data);
            String state = data.substring(10, 12);
            int code = HexUtil.hex16To10(state);
            log.info("code:" + code);
            String imei = data.substring(13, 28);
            log.info("imei:" + imei);
            String bat = data.substring(56, 58);
            log.info("bat:" + bat);
            BigDecimal decimal = new BigDecimal(String.valueOf(HexUtil.hex16To10(bat)));
            int battery = HexUtil.percentage(decimal);
            Device device = new Device();
            device.setImei(imei);
            device.setFg(4);
            device.setBattery(battery);
            deviceService.updateByImeiSelective(device);
            JSONObject jsonData = new JSONObject();
            jsonData.put("code", code);
            jsonData.put("imei", imei);
            publishService.sendToMqtt(jsonData.toJSONString(), EmqCode.sendNbTopic + tupDataReport.getIMEI() + EmqCode.endNbTopic, 2);
        }

        //JSONObject
        YgPayload ygPayload = JSONObject.toJavaObject(json, YgPayload.class);
        if (ygPayload == null) {
            return;
        }
        if (!StringUtils.equals("Alarm", ygPayload.getServiceId())) {
            return;
        }
        YgServiceData ygServiceData = ygPayload.getServiceData();
        if (ygServiceData == null) {
            return;
        }
        log.info("alarmCode:" + ygServiceData.getAlarmCode());
//        GeneralPayload generalPayload = new GeneralPayload();
//        generalPayload.setIMEI(tupDataReport.getIMEI());
//        generalPayload.setAlarmCode(ygServiceData.getAlarmCode());
//        generalPayload.setDeviceType(ygServiceData.getDeviceType());
//        rabbitService.send(JSON.toJSONString(generalPayload), new HashMap<>(), CwtingConstant.QUEUE_YG_NOTIFY, CwtingConstant.EXCHANGE_YG_NOTIFY);
        GeneralNbPayload generalPayload = new GeneralNbPayload();
        generalPayload.setIMEI(tupDataReport.getIMEI());
        generalPayload.setCode(ygServiceData.getAlarmCode());
        generalPayload.setDeviceType(ygServiceData.getDeviceType());
        publishService.sendToMqtt(JSON.toJSONString(generalPayload), EmqCode.sendNbTopic + tupDataReport.getIMEI() + EmqCode.endNbTopic, 2);
    }
}
